SpringMVC_第二章SpringMVC入门代码实现

1:功能概述

用springmvc实现在页面的简单的商品保存功能,页面上有商品名称和价格两个按钮,输入之后提交到后台,并且在新的页面把数据展示出来。

点击提交之后如图所示

2:代码实现如下 

添加页面:

	<form class="form-horizontal" role="form" action="action11/addGoods" method="post">
		<input type="text" class="form-control" name="name" placeholder="请输入商品名称"> 
		<input type="text" class="form-control" name="price" placeholder="请输入商品价格">
		<button type="submit" class="btn btn-default">提交</button>
	</form>

进入Controller

@Controller(value="action1")
@RequestMapping("action11")
public class action1 {
	@Autowired
	private service1 service1;
//	springMVC把前台的数据封装为POJO与struts2的封装形式不同。
//	struts2需要在控制器声明需封装的POJO,而springMVC不需要任何准备工作,
//	只需在相应的方法的参数中加上需封装的POJO,当用户提交表单时,
//	springMVC会根据表单中dom元素的name属性与请求的方法中的参数中的POJO的属性名进行比对,
//	如果相同,
//	则将name属性的值赋给这个属性,进而完成封装,下面举例说明
	@RequestMapping("/addGoods")
	public ModelAndView addgoods(HttpServletRequest request,HttpServletResponse response,goods goods) {
		System.out.println("进入后台方法!!!!");
		ModelAndView andView=new ModelAndView();
		System.out.println(goods.toString());
		System.out.println("name:"+goods.getName());
		andView.addObject("goods", goods);

		andView.addObject(service1.getname(), "11111111111");
		if(goods.getName()==null||("").equals(goods.getName())) {
			System.out.println("name为空!");
			andView.setViewName("add");
		}else {
			System.out.println("name不为空!");
			andView.setViewName("show");
		}
		return andView;
		
	}
	

进入Service:

@Service
public class service1 {
	@Autowired
	private dao1 dao;
	public String getname() {
		System.out.println("--service1--");
		String name=dao.getname();
		return name;
	}

}

进入Dao:

@Repository
public class dao1 {
	public String getname() {
		return "name";
	}
}

展示页面:

<body>
	${name}
	<h1>-----------展示商品名称---------</h1>
	${requestScope.goods.name}
	<h1>-----------展示商品价格---------</h1>
	${goods.price}
</body>

公共web.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	
	
	<!--处理中文乱码  -->
	<filter>
		<filter-name>encofingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
		
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	
	<!-- 加载js,jsp -->
	<filter-mapping>
	<filter-name>encofingFilter</filter-name>
	<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.js</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.css</url-pattern>
	</servlet-mapping>
	
	
	<servlet>
		<!-- Dispatcher Servlet 调度服务器-->
		<servlet-name>SpringMVC1</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!--配置springmvc的初始化路径 
		classpath:只会到你的class路径中查找找文件; 
			classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.
		-->
		 <init-param>
            <param-name>contextConfigLocation</param-name>
           <!--  <param-value>WEB-INF/classes/Spring/springmvc.xml</param-value>  -->
           <!--  <param-value>classpath:Spring/springmvc.xml</param-value> -->
           
            <param-value>WEB-INF/springzhueji/springmvc.xml</param-value>
        </init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>SpringMVC1</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!--将AnnotationHandler自动扫描到IOC容器中-->
	<context:component-scan base-package="com.thit"></context:component-scan>

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
		<!--前置后缀 -->
		<property name="prefix" value="/"></property>
		<!--后置后缀  -->
		<property name="suffix" value=".jsp"></property>
	</bean>

  
</beans>

以上就是这个简单功能的代码实例。

springMVC的注解配置是:

     @RequestMapping("xxxx")

    //@Repository //用于DAO实现类
    
    //@Service  //用于service
    
    //@Controller //用于action(controller)

猜你喜欢

转载自blog.csdn.net/huyiju/article/details/82745993
今日推荐