SpringMVC、SpringMVCXML配置(纯XML⽅式)
1、引⼊SrpingMVC所使⽤的Java包:
cglib-nodep-2.1_3.jar、commons-logging.jar、spring-aspects-4.1.7.RELEASE.jar、spring-beans-4.1.7.RELEASE.jar、spring-context-4.1.7.RELEASE.jar
、spring-core-4.1.7.RELEASE.jar、spring-web-4.1.7.RELEASE.jar、spring-webmvc-4.1.7.RELEASE.jar
2、新建Web项⽬,配置l:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="/2001/XMLSchema-instance" xmlns="java.sun/xml/ns/javaee" xmlns:web="java.sun/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="java.sun/xml/ns/javaee ja  <display-name>springmvc</display-name>
<!-- 配置Spring -->
<listener>
<listener-class>org.t.ContextLoaderListener</listener-class>
</listener>
<!-- 配置SpringMVC的编码格式 -->
<filter>
<filter-name>CharacterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 加载其他的配置⽂件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/l</param-value>
</context-param>
<!-- 配置Spring的前置控制器DispatcherServlet -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置Spring的url后缀 -->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3、配置l(基中的部分MVC的配置可另外再分⼀个l配置,这⾥为了⽅便统⼀放在⼀个⾥⾯):
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:mvc="/schema/mvc"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-4.1.xsd
/schema/context
/schema/context/spring-context-4.1.xsd
/schema/mvc
/schema/mvc/spring-mvc-4.1.xsd">
<!-- controller⽅法调⽤规则:通过url中的method参数指定要执⾏的controller⽅法-->
<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action" />
<property name="defaultMethodName" value="view" />
</bean>
<!-- 配置MVC的前缀(prefix)和后缀(suffix) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<!--  <property name="prefix" value="/WEB-INF/" /> -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 以下是控制器的配置 -->
<bean name="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<property name="order" value="1" />
<property name="mappings">
<props>
<prop key="user.do">userController</prop>
<prop key="index.do">indexController</prop>
</props>
</property>
</bean>
<bean name="userController"  class="ller.UserController">
<property name="userIService" ref="userIService"></property>
</bean>
<bean name="indexController"  class="ller.IndexController" />
<!-- ServiceBean的配置 -->
<bean name="userIService" class="cn.raffaello.service.impl.UserServiceImpl" />
</beans>
4、新建Model,User:
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
5、新建ServiceBean并实现⼀个模拟的数据保存添加操作:
public interface UserIService {
void saveOrUpdate(User user);
User getUserById(Integer id);
void delete(Integer id);
List<User> getUsers();
Integer getNewId();
}
public class UserServiceImpl implements UserIService {
private Map<Integer,Object> map=new HashMap<Integer,Object>(); private Map<String,Integer> idCount=new HashMap<String,Integer>(); @Override
public void saveOrUpdate(User user) {
map.Id(), user);
}
@Override
public User getUserById(Integer id) {
ainsKey(id)){
return ((id);
}
return new User();
springmvc考试选择题}
@Override
public void delete(Integer id) {
}
@Override
public List<User> getUsers() {
List<User> list=new ArrayList<User>();
for(Integer key : map.keySet()){
list.add(((key));
}
return list;
}
@Override
public Integer getNewId() {
if(!ainsKey("ids")){
Integer id=1;
idCount.put("ids", id);
return id;
}else{
Integer ("ids");
id=id+1;
idCount.put("ids", id);
return id;
}
}
}
6、新建控制器UserController:
public class UserController implements Controller{
private UserIService userIService;
@Override
public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse res) throws Exception {
String param="";
Parameter("param");
//新增控制器跳转
im().equals("add")){
return new ModelAndView("/WEB-INF/userAdd");
}else im().equals("edit")){
//编辑控制器跳转
Integer id=-1;
if(!Parameter("userId").trim().equals("")){
id=Integer.Parameter("userId").trim());
}
User UserById(id);
req.setAttribute("user", user);
return new ModelAndView("/WEB-INF/userAdd");
}else im().equals("userList")){
//列表页⾯跳转
List<User> Users();
req.setAttribute("userList", list);
return new ModelAndView("/WEB-INF/userList");
}else im().equals("save")){
//新增的保存操作
User user=new User();
String Parameter("name").trim();
user.NewId());
user.setName(name);
userIService.saveOrUpdate(user);
List<User> Users();
req.setAttribute("userList", list);
return new ModelAndView("/WEB-INF/userList");
}else im().equals("update")){
//修改的保存操作
Integer id=-1;
if(!Parameter("userId").trim().equals("")){
id=Integer.Parameter("userId").trim());
}
String Parameter("name").trim();
User UserById(id);
user.setName(name);
userIService.saveOrUpdate(user);
List<User> Users();
req.setAttribute("userList", list);
return new ModelAndView("/WEB-INF/userList");
}else im().equals("delete")){
//删除操作
Integer id=-1;
if(!Parameter("userId").trim().equals("")){
id=Integer.Parameter("userId").trim());
}
userIService.delete(id);
List<User> Users();
req.setAttribute("userList", list);
return new ModelAndView("/WEB-INF/userList");
}else im().equals("detail")){
//详细⽤户页⾯
Integer id=-1;
if(!Parameter("userId").trim().equals("")){
id=Integer.Parameter("userId").trim());
User UserById(id);
req.setAttribute("user", user);
}
return new ModelAndView("/WEB-INF/userDetail");
}else im().equals("list")){
// list列表
List<User> Users();
req.setAttribute("userList", list);
return new ModelAndView("/WEB-INF/userList");
}
return new ModelAndView("fail");
//return forword("stu/studentList", context);  //同样也可以使⽤forword进⾏页⾯跳转
//使⽤Map向前台输出数据时,获取操作同Request⼀样,可以在前台使⽤${名字}来获取如下例,可以使⽤${message}获取  //Map<String,String> resultMap = new HashMap<String,String>();
//resultMap.put("message", "欢迎来到⾸页111");
//return new ModelAndView("index",resultMap);
}
public UserIService getUserIService() {
return userIService;
}
public void setUserIService(UserIService userIService) {
this.userIService = userIService;
}
}
7、然后就是页⾯啦:
新增页⾯:
<body>
<a href="user.do?param=userList">⽤户列表</a>
<h3>新增⽤户</h3>
<form action="user.do" method="post">
<c:if test="${user!=null }">
<input type="hidden" name="param" value="update" />
</c:if>
<c:if test="${user==null }">
<input type="hidden" name="param" value="save" />
</c:if>
<input type="hidden" name="userId" value="${user.id }" />
⽤户名:<input type="text" name="name" value="${user.name }"  />
<input type="submit" value="提交" />
</form>
</body>
访问地址:localhost/user.do?param=add
修改页⾯也是此页⾯,访问地焉localhost/user.do?param=edit&userId=12
⽤户信息页⾯:
<body>
<a href="user.do?param=userList">⽤户列表</a>
<h3>⽤户详细信息:</h3>
<ul>
<li>ID:${user.id }</li>
<li>⽤户名:${user.name }</li>
</ul>
</body>
⽤户列表页⾯:
<body>
<a href="user.do?param=add">新增⽤户</a>
<h3>⽤户列表:</h3>
<table>
<tr>
<td>ID</td>
<td>姓名</td>
<td>操作</td>
</tr>
<c:forEach items="${userList }" var="user">
<tr>
<td>${user.id }</td>
<td>${user.name }</td>
<td><a href="user.do?param=edit&userId=${user.id }">修改</a></td>
</tr>
</c:forEach>
</table>
</body>
下图是效果图:
其他的SpringMVC⽂档:
Spring各个版本下载:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。