<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/images/**" location="/resources/images/" />
<resources mapping="/css/**" location="/resources/css/" />
<resources mapping="/js/**" location="/resources/js/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
enctype="multipart/form-data"
используйте библиотеку Commons FileUpload. Там документация нормaction="${pageContext.request.contextPath}/do/add-product"
лучше использовать jstl core, тег <c:url/>
. Например: <c:url value="/do/add-product"/>
Dog tuzic = new Dog();
{
tuzic.age = 1;
}
class Dog extends Pet {
Dog tuzic = new Dog();
}
.....
Dog tuzic = new Dog(); // и понеслась:)
import java.io.*;
public class CharCleaner {
public static void main(String[] args) throws IOException {
try (
Reader reader = new BufferedReader(new FileReader(new File("sourceFile.txt")));
Writer writer = new BufferedWriter(new FileWriter(new File("resultFile.txt")))
) {
int ch;
while ((ch = reader.read()) != -1) {
if (Character.isAlphabetic(ch)) {
writer.write(ch);
}
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
interface Worker {
void doWork(int[] data);
}
class Worker96 implements Worker {
public void doWork(int[] data) {
....
}
}
class Worker71 implements Worker {
public void doWork(int[] data) {
....
}
}
// потом можно создать мапу
class Main {
private Map<Integer, Worker> workers = ....;
Main (){
workers.put(96, new Worker96());
workers.put(71, new Worker71());
}
public void dataProcessing(int[] data) {
Worker w = workers.get(data.length);
if (w == null)
throw new RuntimeException("Unsupported data length!");
w.doWork(data);
}
}
// как то так...
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.io.IOException;
public class TestJackson {
Integer a;
public Integer getA() {
return a;
}
@JsonDeserialize(using = StringIntegerDeserializer.class)
public void setA(Integer a) {
this.a = a;
}
@Override
public String toString() {
return "TestJackson{a=" + a + "}";
}
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
String json = "{\"a\":\"5.666\"}";
System.out.println(mapper.readValue(json, TestJackson.class));
json = "{\"a\":5.666}";
System.out.println(mapper.readValue(json, TestJackson.class));
json = "{\"a\":5}";
System.out.println(mapper.readValue(json, TestJackson.class));
}
public static class StringIntegerDeserializer extends JsonDeserializer<Integer> {
@Override
public Integer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
String valueAsString = jsonParser.getValueAsString();
int integer = (int) Double.parseDouble(valueAsString);
return integer;
}
}
}
<form ...>
<input name="lastName" value="Ivanov" />
<input name="firstName" value="Ivan" />
<input name="lastName" value="Petrov" />
<input name="firstName" value="Petr" />
</form>
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// тут будит lastNames = {"Ivanov", "Petrov"}
String[] lastNames = request.getParameterValues("lastName");
// тут будит firstNames = {"Ivan", "Petr"}
String[] firstNames = request.getParameterValues("firstName");
List<Author> author = new ArrayList<Author>();
for (int i = 0; i < lastNames.length && i < firstNames.length; i ++) {
author.add(new Author(firstNames[i], lastNames[i]));
}
}
@WebServlet(urlPatterns = "/home") // javax.servlet-api 3.0
public class HomeServlet extends HttpServlet {
private final Gson gson = new Gson();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<String> users = Arrays.asList("Vaya", "Petya", "Fedya");
String json = gson.toJson(users);
req.setAttribute("usersJson", json);
req.getRequestDispatcher("/home.jsp").forward(req,resp);
}
}
<html>
<head>
<script src="js/jquery-1.9.1.js"></script>
</head>
<body>
<script>
var users = ${usersJson};
$.each(users, function(i, val) {
.....................
});
</script>
</body>
</html>
@WebServlet(urlPatterns = "/json")
public class JsonServlet extends HttpServlet {
private final Gson gson = new Gson();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<String> users = Arrays.asList("Vaya", "Petya", "Fedya");
resp.addHeader("Content-Type", "application/json");
String json = gson.toJson(users);
resp.getWriter().print(json);
resp.flushBuffer();
}
}
<html>
<head>
<script src="js/jquery-1.9.1.js"></script>
</head>
<body>
<script>
$.get('json', function(data) {
$.each(data, function(i, val) {
..............................
});
});
</script>
</body>
</html>